var pageii = null;
var grid = null;
$(function() {
	grid = lyGrid({
		id : 'paging',
		l_column : [ 
	        { colkey : "role_id", name : "id", width : "50px", hide : true }, 
			{colkey : "role_name",name : "名称",width : "200px",align: "left" }, 
			{ colkey : "role_code", name : "标识", width : "200px" ,align: "left" }, 
			{ colkey : "sequence", name : "排序", width : "150px" }, 
			{ colkey : "create_time", name : "创建时间", width : "150px",
				renderData : function(rowindex, data, rowdata, column) {
					return new Date(data).format("yyyy-MM-dd hh:mm");
				}
			}, 
			{ colkey : "description", name : "描述", align : "left" } 
		],
		jsonUrl : rootPath + '/system/roleInfo/findByPage.shtml',
		checkbox : true,
		serNumber : true,
		singleCheck:true,		
		checkValue : 'role_id'
	});
	$("#search").click("click", function() {// 绑定查询按扭
		var searchParams = $("#searchForm").serializeJson();// 初始化传参数
		grid.setOptions({
			data : searchParams
		});
	});
	$("#addRole").click("click", function() {
		addRole();
	});
	$("#editRole").click("click", function() {
		editRole();
	});
	$("#delRole").click("click", function() {
		delRole();
	});
	$("#permissions").click("click", function() {
		permissions();
	});

	$("#addUserRole").click("click", function() {
		addUserRole();
	});

});
function editRole() {
	var cbox = grid.getSelectedCheckbox();
	if (cbox.length > 1 || cbox == "") {
		tips("只能选中一个",1);
		return;
	}
	if (cbox == 'administrator' || cbox == 'everyone') {
		tips("系统角色不可编辑",1);
		return;
	}
	showModal("role_info", "编辑", rootPath + "/system/roleInfo/editUI.shtml?role_id="
			+ cbox, 720, 350);
}
function permissions() {
	var cbox = grid.getSelectedCheckbox();
	if (cbox.length > 1 || cbox == "") {
		tips("请选择一个角色",1);
		return;
	}
	if (cbox == 'administrator') {
		tips("系统管理员角色无法授权",1);
		return;
	}
	showModal("set_power_info", "设置权限", rootPath
			+ "/roleInfo/permissions.shtml?role_id=" + cbox, 350, 450);
}

function addUserRole() {
	var cbox = grid.getSelectedCheckbox();
	if (cbox.length > 1 || cbox == "") {
		tips("请选择一个角色",1);
		return;
	}else if(cbox=='everyone'){
		tips("所有人角色不需要关联用户",1);
		return;
	}
	showModal("user_role_info", "关联用户", rootPath
			+ "/system/roleInfo/findUserRole.shtml?role_id=" + cbox, 820, 620);

}

function addRole() {
	showModal("role_info", "新增", rootPath + "/system/roleInfo/addUI.shtml", 720, 350);
}
function delRole() {
	var cbox = grid.getSelectedCheckbox();
	if (cbox == "") {
		tips("请选择需要删除的角色",1);
		return;
	}
	for (var i = 0; i < cbox.length; i++) {
		if (cbox[i] == 'administrator' || cbox[i] == 'everyone') {
			tips("系统角色不可删除",1);
			return;
		}
	}
	confirm('你确定要删除吗？', function() {
		var url = rootPath + '/system/roleInfo/deleteEntity.shtml';
		var s = CommnUtil.ajax(url, {
			ids : cbox.join(",")
		}, "json");
		if (s == "success") {
			tips("删除成功", 1);
			loadData();
		} else {
			tips("删除失败", 1);
		}
	});
}

// 重新加载数据
function loadData() {
	var searchParams = $("#searchForm").serializeJson();// 初始化传参数
	grid.setOptions({
		data : searchParams
	});
}
